﻿namespace SERFLITEMAX20.WinUI.Controls
{
    using SERFLITEMAX20.Core;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class frmScanner : Form
    {
        private Button btnClear;
        private Button btnListValues;
        private Button btnSearch;
        private ComboBox cboSearchType;
        private ColumnHeader columnHeader10;
        private ColumnHeader columnHeader8;
        private IContainer components = null;
        private Label label2;
        private Label label3;
        private Label lblBase;
        private ListView lstView;
        private TextBox mfind;
        private TextBox mFrom;
        private ListViewSortManager mListViewSortManager = null;
        private Main mMain = null;
        private TextBox mPtrBase;
        private TextBox mTo;

        public frmScanner()
        {
            this.InitializeComponent();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.lstView.Items.Clear();
        }

        private void btnListValues_Click(object sender, EventArgs e)
        {
            this.lstView.Items.Clear();
            try
            {
                this.mListViewSortManager = new ListViewSortManager(this.lstView, new System.Type[] { typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort) });
                int ptrBase = this.PtrBase;
                int fromPtr = this.FromPtr;
                int toPtr = this.ToPtr;
                for (int i = fromPtr; i <= toPtr; i++)
                {
                    ListViewItem item = this.lstView.Items.Add(i.ToString());
                    string str = "";
                    string text = this.mMain.Memory.ReadInteger(ptrBase + i).ToString();
                    item.SubItems.Add(text);
                    str = "INT:" + text;
                    text = this.mMain.Memory.ReadFloat(ptrBase + i).ToString();
                    item.SubItems.Add(text);
                    str = str + "\tFLOAT:" + text;
                    text = this.mMain.Memory.ReadLong(ptrBase + i).ToString();
                    item.SubItems.Add(text);
                    str = str + "\tLONG:" + text;
                    if (!this.mfind.Text.Equals(text))
                    {
                        item.Remove();
                    }
                    else
                    {
                        int address = this.mMain.Memory.ReadInteger(ptrBase + i);
                        string str3 = "";
                        if (address != 0)
                        {
                            address = this.mMain.Memory.ReadInteger(address + 8);
                            if (address != 0)
                            {
                                str3 = this.mMain.Memory.ReadString(address, 0x40);
                            }
                        }
                        str = str + "\tSTRING:" + str3;
                        item.SubItems.Add(str3);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.lstView.Items.Clear();
            if (this.cboSearchType.SelectedItem != null)
            {
                string str = this.cboSearchType.SelectedValue.ToString();
                if (str != null)
                {
                    if (!(str == "1"))
                    {
                        if (str == "2")
                        {
                            this.SearchInt();
                        }
                        else if (str == "3")
                        {
                            this.SearchFloat();
                        }
                        else if (str == "4")
                        {
                            this.SearchString();
                        }
                    }
                    else
                    {
                        this.SearchLong();
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.mTo = new System.Windows.Forms.TextBox();
            this.mFrom = new System.Windows.Forms.TextBox();
            this.btnClear = new System.Windows.Forms.Button();
            this.btnListValues = new System.Windows.Forms.Button();
            this.columnHeader10 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
            this.lstView = new System.Windows.Forms.ListView();
            this.lblBase = new System.Windows.Forms.Label();
            this.mPtrBase = new System.Windows.Forms.TextBox();
            this.mfind = new System.Windows.Forms.TextBox();
            this.btnSearch = new System.Windows.Forms.Button();
            this.cboSearchType = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(219, 35);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(33, 20);
            this.label2.TabIndex = 13;
            this.label2.Text = "To";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(6, 35);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(37, 20);
            this.label3.TabIndex = 12;
            this.label3.Text = "From";
            // 
            // mTo
            // 
            this.mTo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.mTo.Location = new System.Drawing.Point(258, 35);
            this.mTo.Name = "mTo";
            this.mTo.Size = new System.Drawing.Size(147, 20);
            this.mTo.TabIndex = 10;
            // 
            // mFrom
            // 
            this.mFrom.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.mFrom.Location = new System.Drawing.Point(49, 35);
            this.mFrom.Name = "mFrom";
            this.mFrom.Size = new System.Drawing.Size(163, 20);
            this.mFrom.TabIndex = 11;
            // 
            // btnClear
            // 
            this.btnClear.Location = new System.Drawing.Point(607, 2);
            this.btnClear.Name = "btnClear";
            this.btnClear.Size = new System.Drawing.Size(94, 26);
            this.btnClear.TabIndex = 8;
            this.btnClear.Text = "Clear";
            this.btnClear.UseVisualStyleBackColor = true;
            this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
            // 
            // btnListValues
            // 
            this.btnListValues.Location = new System.Drawing.Point(507, 2);
            this.btnListValues.Name = "btnListValues";
            this.btnListValues.Size = new System.Drawing.Size(94, 26);
            this.btnListValues.TabIndex = 9;
            this.btnListValues.Text = "List Values";
            this.btnListValues.UseVisualStyleBackColor = true;
            this.btnListValues.Click += new System.EventHandler(this.btnListValues_Click);
            // 
            // columnHeader10
            // 
            this.columnHeader10.Text = "Value";
            this.columnHeader10.Width = 388;
            // 
            // columnHeader8
            // 
            this.columnHeader8.Text = "Row";
            this.columnHeader8.Width = 50;
            // 
            // lstView
            // 
            this.lstView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lstView.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lstView.CheckBoxes = true;
            this.lstView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader8,
            this.columnHeader10});
            this.lstView.FullRowSelect = true;
            this.lstView.GridLines = true;
            this.lstView.HideSelection = false;
            this.lstView.Location = new System.Drawing.Point(2, 79);
            this.lstView.Name = "lstView";
            this.lstView.Size = new System.Drawing.Size(715, 333);
            this.lstView.TabIndex = 7;
            this.lstView.UseCompatibleStateImageBehavior = false;
            this.lstView.View = System.Windows.Forms.View.Details;
            this.lstView.DoubleClick += new System.EventHandler(this.lstView_DoubleClick);
            // 
            // lblBase
            // 
            this.lblBase.Location = new System.Drawing.Point(6, 9);
            this.lblBase.Name = "lblBase";
            this.lblBase.Size = new System.Drawing.Size(37, 20);
            this.lblBase.TabIndex = 15;
            this.lblBase.Text = "Base";
            // 
            // mPtrBase
            // 
            this.mPtrBase.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.mPtrBase.Location = new System.Drawing.Point(49, 9);
            this.mPtrBase.Name = "mPtrBase";
            this.mPtrBase.Size = new System.Drawing.Size(163, 20);
            this.mPtrBase.TabIndex = 14;
            // 
            // mfind
            // 
            this.mfind.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.mfind.Location = new System.Drawing.Point(411, 35);
            this.mfind.Name = "mfind";
            this.mfind.Size = new System.Drawing.Size(285, 20);
            this.mfind.TabIndex = 10;
            // 
            // btnSearch
            // 
            this.btnSearch.Location = new System.Drawing.Point(407, 2);
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Size = new System.Drawing.Size(94, 26);
            this.btnSearch.TabIndex = 9;
            this.btnSearch.Text = "Search";
            this.btnSearch.UseVisualStyleBackColor = true;
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            // 
            // cboSearchType
            // 
            this.cboSearchType.FormattingEnabled = true;
            this.cboSearchType.Location = new System.Drawing.Point(258, 8);
            this.cboSearchType.Name = "cboSearchType";
            this.cboSearchType.Size = new System.Drawing.Size(143, 21);
            this.cboSearchType.TabIndex = 17;
            // 
            // frmScanner
            // 
            this.ClientSize = new System.Drawing.Size(716, 416);
            this.Controls.Add(this.cboSearchType);
            this.Controls.Add(this.lblBase);
            this.Controls.Add(this.mPtrBase);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.mfind);
            this.Controls.Add(this.mTo);
            this.Controls.Add(this.mFrom);
            this.Controls.Add(this.btnClear);
            this.Controls.Add(this.btnSearch);
            this.Controls.Add(this.btnListValues);
            this.Controls.Add(this.lstView);
            this.Name = "frmScanner";
            this.Text = "frmScanner";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void lstView_DoubleClick(object sender, EventArgs e)
        {
            if (this.lstView.SelectedItems.Count > 0)
            {
                this.mMain.RaiseAddWatchEvent(this.PtrBase, int.Parse(this.lstView.SelectedItems[0].SubItems[0].Text), ((DataRowView) this.cboSearchType.SelectedItem)["Name"].ToString());
            }
        }

        private void SearchFloat()
        {
            int ptrBase = this.PtrBase;
            int fromPtr = this.FromPtr;
            int toPtr = this.ToPtr;
            float num4 = float.Parse(this.mfind.Text);
            for (int i = fromPtr; i <= toPtr; i += 4)
            {
                float num6 = this.mMain.Memory.ReadFloat(ptrBase + i);
                if (num6 == num4)
                {
                    this.lstView.Items.Add(i.ToString()).SubItems.Add(num6.ToString());
                }
            }
        }

        private void SearchInt()
        {
            int ptrBase = this.PtrBase;
            int fromPtr = this.FromPtr;
            int toPtr = this.ToPtr;
            long num4 = int.Parse(this.mfind.Text);
            if (this.mMain != null)
            {
                for (int i = fromPtr; i <= toPtr; i += 4)
                {
                    int num6 = this.mMain.Memory.ReadInteger(ptrBase + i);
                    if (num6 == num4)
                    {
                        this.lstView.Items.Add(i.ToString()).SubItems.Add(num6.ToString());
                    }
                }
            }
        }

        private void SearchLong()
        {
            try
            {
                int ptrBase = this.PtrBase;
                int fromPtr = this.FromPtr;
                int toPtr = this.ToPtr;
                long num4 = long.Parse(this.mfind.Text);
                for (int i = fromPtr; i <= toPtr; i += 8)
                {
                    long num6 = this.mMain.Memory.ReadLong(ptrBase + i);
                    if (num6 == num4)
                    {
                        this.lstView.Items.Add(i.ToString()).SubItems.Add(num6.ToString());
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void SearchString()
        {
            int ptrBase = this.PtrBase;
            int fromPtr = this.FromPtr;
            int toPtr = this.ToPtr;
            string text = this.mfind.Text;
            if (this.mMain != null)
            {
                for (int i = fromPtr; i <= toPtr; i++)
                {
                    int address = this.mMain.Memory.ReadInteger(ptrBase + i);
                    string str2 = "";
                    if (address != 0)
                    {
                        address = this.mMain.Memory.ReadInteger(address);
                        if (address != 0)
                        {
                            str2 = this.mMain.Memory.ReadString(address, 0x40);
                        }
                    }
                    if (str2.IndexOf(text) != -1)
                    {
                        this.lstView.Items.Add(i.ToString()).SubItems.Add(str2);
                    }
                }
            }
        }

        private void UpdateViewer()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name");
            table.Columns.Add("Value");
            table.Rows.Add(new object[] { "Long", "1" });
            table.Rows.Add(new object[] { "Int", "2" });
            table.Rows.Add(new object[] { "Float", "3" });
            table.Rows.Add(new object[] { "String", "4" });
            this.cboSearchType.DisplayMember = "Name";
            this.cboSearchType.ValueMember = "Value";
            this.cboSearchType.DataSource = table;
        }

        public void View(Main pMain)
        {
            this.mMain = pMain;
            this.UpdateViewer();
            base.Show();
        }

        private int FromPtr
        {
            get
            {
                try
                {
                    return int.Parse(this.mFrom.Text);
                }
                catch
                {
                    return 0;
                }
            }
        }

        private int PtrBase
        {
            get
            {
                try
                {
                    return int.Parse(this.mPtrBase.Text);
                }
                catch
                {
                    return 0;
                }
            }
        }

        private int ToPtr
        {
            get
            {
                try
                {
                    return int.Parse(this.mTo.Text);
                }
                catch
                {
                    return 0;
                }
            }
        }
    }
}

